package com.zhs.gccs.view.taskMap;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhs.gccs.R;
import com.zhs.gccs.model.TaskProject;
import com.zhs.gccs.adapter.ProjectInfoAdapter;
import com.zhs.gccs.view.BaseView;
import com.zhs.gccs.utils.DoubleOperation;

import java.util.ArrayList;
import java.util.List;

public class SearchResultView extends BaseView {
    private View ll_search_list;//搜索
    private View searchResultView;
    private TextView tv_xmzs;//项目总数
    private TextView tv_jszmj;//建设总面积
    private TextView tv_xzjsggmj;//新增节水灌溉面积
    private TextView tv_tzze;//投资总额
    private TextView tv_xzgdmj;//新增耕地面积
    private RecyclerView rv_search;// 项目列表
    private ProjectInfoAdapter projectInfoAdapter;

    private List<TaskProject> taskProjects;//项目

    public SearchResultView(@NonNull Context context) {
        super(context);
    }

    public SearchResultView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SearchResultView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View searchResultView = LayoutInflater.from(context).inflate(R.layout.layout_view_search_result, this, true);
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomerLogo);
//        String title = typedArray.getString(R.styleable.CustomerInfoItem_preText);
//        typedArray.recycle();
        ll_search_list = searchResultView.findViewById(R.id.ll_search_list);
        tv_xmzs = searchResultView.findViewById(R.id.tv_xmzs);
        tv_jszmj = searchResultView.findViewById(R.id.tv_jszmj);
        tv_xzjsggmj = searchResultView.findViewById(R.id.tv_xzjsggmj);
        tv_tzze = searchResultView.findViewById(R.id.tv_tzze);
        tv_xzgdmj = searchResultView.findViewById(R.id.tv_xzgdmj);
        rv_search = searchResultView.findViewById(R.id.rv_search);
    }

    public void loadData(List<TaskProject> data){
        this.taskProjects = new ArrayList<>();
        if(data!=null)this.taskProjects.addAll(data);
        rv_search.setLayoutManager(new LinearLayoutManager(context));
        projectInfoAdapter = new ProjectInfoAdapter(context,this.taskProjects);
        rv_search.setAdapter(projectInfoAdapter);
        projectInfoAdapter.setOnItemListener(new ProjectInfoAdapter.OnItemListener() {
            @Override
            public void onItemClick(int position) {
                if(searchViewListener!=null)searchViewListener.onProjectSelected(taskProjects.get(position));
            }
            @Override
            public void onItemLongClick(int position) {
                if(searchViewListener!=null)searchViewListener.onProjectLongClick(taskProjects.get(position));
            }
        });
        Double dobJszmj = 0.0;
        Double dobTzze = 0.0;
        Double dobXzgdmj = 0.0;
        Double dobzjsggmj = 0.0;
        for (int i = 0; i < this.taskProjects.size(); i++) {
            TaskProject taskProject = this.taskProjects.get(i);
            if (null != taskProject.getpJsgm()) {
                dobJszmj = DoubleOperation.add(dobJszmj, taskProject.getpJsgm()!=null?Double.parseDouble(taskProject.getpJsgm()):0d);
                dobTzze = DoubleOperation.add(dobTzze, taskProject.getPztz()!=null?Double.parseDouble(taskProject.getPztz()):0d);
                dobXzgdmj = DoubleOperation.add(dobXzgdmj, taskProject.getPnewmj()!=null?Double.parseDouble(taskProject.getPnewmj()):0d);
                dobzjsggmj = DoubleOperation.add(dobzjsggmj, taskProject.getPnewjsmj()!=null?Double.parseDouble(taskProject.getPnewjsmj()):0d);
            }
        }
        tv_xmzs.setText(this.taskProjects.size()+"");
        tv_jszmj.setText(dobJszmj + "");
        tv_xzjsggmj.setText(dobzjsggmj + "");
        tv_tzze.setText(dobTzze + "");
        tv_xzgdmj.setText(dobXzgdmj + "");
    }
    private SearchResultViewListener searchViewListener;

    public void setSearchViewListener(SearchResultViewListener searchViewListener) {
        this.searchViewListener = searchViewListener;
    }

    public interface SearchResultViewListener{
        void onProjectSelected(TaskProject taskProject);
        void onProjectLongClick(TaskProject taskProject);
    }

}
